<template>
	<view class="pages">
		<view style="padding:20rpx;">
			<u-upload :fileList="fileList1" :imageMode="imageMode" @afterRead="afterRead" @delete="deletePic" name="6" multiple :maxCount="maxCount"
				width="710" height="340">
				<!-- <image :src="`${$http_cos}/static/%E5%A2%9E%E5%8A%A0%E7%85%A7%E7%89%87%E6%95%B4%E4%BD%93.png`"
					mode="widthFix" style="width: 202rpx;height:202rpx;" v-if="fileList.length==0"  ></image> -->
				<view class="card_imgs">
					<image :src="`${$http_cos}/static/card_img.png`" mode="aspectFit"
						style="width: 710rpx;height:340rpx;">
					</image>
				</view>
			</u-upload>
		</view>
		
		<!-- <view class="public">
			<text>发布</text>
		</view> -->

		<view class="btn_public">
			<u-button text="发布"
				customStyle="border-radius:46rpx;background:#FF7612;border:2rpx solid #FF7612;color:#fff"
				@click="submit"></u-button>
			<!-- <u-button type="primary" disabled text="发布"
				customStyle="border-radius:46rpx;background:#D3D4D6;border:2rpx solid #D3D4D6;color:#fff" v-else>
			</u-button> -->
		</view>
	</view>
</template>

<script>
	import COS from 'cos-js-sdk-v5';
	import cos from '@/common/cos.js'
	// const fs = require('fs');
	const app = getApp();
	import {
		saveAdvert
	} from '@/common/api.js'
	export default {
		data() {
			return {
				imageMode:'aspectFill',
				maxCount:1,
				// content: '',
				fileList1: [],
				$http_cos: app.$http_cos,
			}
		},
		onLoad() {

		},
		methods: {
			async afterRead(event) {
				console.log(event)
				let fileList = this.fileList1
				// console.log(fileList)
				let lists = [].concat(event.file)
				let arr = []
				let fileListLen = this.fileList1.length
				let Images = [];
				console.log(lists)
				// 遍历上传的每张图片
				for (let i = 0; i < lists.length; i++) {
					
					const item = lists[i];
					console.log(item)
					// 验证图片格式
					const isImage = /\.(png|jpe?g)$/i.test(item.name);
					if (!isImage) {
						uni.showToast({
							title: '只能上传png，jpg，jpeg格式的图片',
							icon: 'none',
							duration: 3000
						});
						lists.splice(i, 1);
						continue;
					}
					// 验证图片大小
					// const maxSize = 2 * 1024 * 1024; // 2MB大小限制
					// if (item.size > maxSize) {
					// 	uni.showToast({
					// 		title: '图片大小不能超过2MB',
					// 		icon: 'none',
					// 		duration: 3000
					// 	});
					// 	// 删除不符合条件的图片
					// 	lists.splice(i, 1);
					// 	// 跳过当前图片，进行下一张图片的验证
					// 	continue;
					// }
					Images.push(item)
				}
			
				Images.forEach((item) => {
					if (fileList.length > Number(this.maxCount)) {
						return uni.$u.toast('只能上传' + this.maxCount + '张')
					}
					this.fileList1.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				// this.fileList1 = Images
				// let fileList2 = this.fileList1
				for (let i = 0; i < Images.length; i++) {
					let name = Images[i].name
					let index = Images[i].name.indexOf('.')
					let name1 = name.substring(0, index)
					let name2 = name.substring(index, name.length)
					Images[i].name = name1 + cos.uuid() + name2
					let blob = await cos.objectURLToBlob(Images[i].url, Images[i].name);
					
					const result = await cos.uploadFilePromise(blob, Images[i].name);
			
					let item = this.fileList1[fileListLen]
					fileList.splice(fileListLen, 1, Object.assign(item, {
						status: result.status,
						message:result.status=='failed'? '上传失败' : '',
						url: 'https://' + result.url
					}))
					this.fileList1 = fileList
					console.log(this.fileList1)
					fileListLen++
				}
				// this.$forceUpdate(sss)
			},
			deletePic(event) {
				let fileList = this.fileList1
				uni.showModal({
					title: '提示',
					content: '是否要删除图片',
					success: (res) => {
						if (res.confirm) {
							fileList.splice(event.index, 1)
							this.fileList1 = fileList
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			
			},
			submit(e) {
				let isclick = true
			let image=this.fileList1.map(item=>{
					return item.url
				})
				if (isclick) {
					isclick = false
					uni.showLoading({
						title: '加载中'
					});
					saveAdvert({
						image: image.join(','),
						
					}, {
						custom: {
							auth: true,
							toast: false,
							catch: true
						}
					}).then((r) => {
						isclick = true
						uni.$u.toast('发布成功')
						setTimeout(function() {
							uni.switchTab({
								url: "/pages/index/index"
							})
						}, 2000);
						uni.hideLoading();
					}).catch((err) => {
						uni.$u.toast(err.message)
						uni.hideLoading();
					})
				}

			}
		},
		
	}
</script>

<style lang="scss">
	.pages {
		background-color: #fff;
		height: 100vh;

		.btn_public {
			position: fixed;
			bottom: 134rpx;
			left: 55rpx;
			right: 55rpx;
			line-height: 92rpx;
			overflow: hidden;
		}
	}
</style>